app.fight = {
	_lastEnemyType : '',
	_lastArea : null,
	_lastAreaEnemies : [],
	_log : null,
	
	/**
	 * Fight module init
	 */
	init : function() {
		var that = this;
		this._log = $('<div>').attr('id', '#battleLog');
		
		$('#locationBattleActions').show();
		$('#locationBattleActions .againLink').click(function() {
			that.fightAgain();
			
			return false;
		});
		$('#locationBattleActions .sameAgainLink').click(function() {
			that.fightSameMonsterAgain();
			
			return false;
		});
	},
	
	/**
	 * Start new battle in current location
	 *
	 * @param enemyType List of available enemy types to select from
	 * @param area Current area
	 */
	start : function(enemyType, area) {
		this._lastArea = area || this._lastArea;
		this._lastAreaEnemies = this._lastArea.enemyTypes || this._lastAreaEnemies;
		this._lastEnemyType = enemyType;
		
		if (typeof enemyType == 'object' && enemyType.length > 0) {
			this._lastEnemyType = enemyType[parseInt(Math.random() * enemyType.length)];
		}
		
		if (app.player.isDead()) {
			this.add2log('You need to rest!');
			
			return false;
		}
		
		var enemy = app.UnitFactory.create(this._lastEnemyType);
		
		this._lastArea.enemies.push(enemy);
		
		return this.doFight(enemy);
	},
	
	/**
	 * Initialize battle sequence
	 *
	 * @param enemy Enemy unit
	 * @param "true" if player wins
	 */
	doFight : function(enemy) {
		var winner = null;
		var looser = null;
		var draft = false;
		var s1, s2;
		
		if (app.player.getAttr('speed') > enemy.getAttr('speed')) {
			s1 = app.player;
			s2 = enemy;
		}
		else {
			s1 = enemy;
			s2 = app.player;
		}
		
		this.add2log(s1.name + ' attacks ' + s2.name + '!');
		
		do {
			this.hit(s1, s2);
			
			if (s1.isDead() && s2.isDead()) {
				draft = true;
			}
			else if (s1.isDead()) {
				winner = s2;
				looser = s1;
			}
			else if (s2.isDead()) {
				winner = s1;
				looser = s2;
				
				this._lastArea.enemies.pop();
			}
			else {
				var tmp = s1;
				s1 = s2;
				s2 = tmp;
			}
		} while (!winner && !draft);
		
		this.add2log(draft ? 'Draft!' : (winner.name + ' win!'));
		
		if (!draft) {
			var fightExp = parseInt(looser.getAttr('exp') / 10) || 1;
			
			winner.addExp(fightExp);
		}
		
		app.area.add2stage(this._log);
		
		return (winner == app.player);
	},
	
	/**
	 * Hit the other side
	 *
	 * @param s1 Unit 1
	 * @param s2 Unit 2
	 */
	hit : function(s1, s2) {
		if (!s1 || !s2) {
			return;
		}
		
		if (s1.getAttr('accuracy') > (Math.random() * 100)) {
			var damage = s1.getDamage() - s2.getDefence();
			
			if (damage > 0) {
				s2.modifyAttr('health', -damage);
				
				this.add2log(s1.name + ' hits ' + s2.name + ' for ' + damage + ' damage (' + s2.getAttrString('health', false) + ' health)');
			}
			else {
				this.add2log(s1.name + ' hit ' + s2.name + ', but attack was too weak');
			}
		}
		else {
			this.add2log(s1.name + ' failed to hit ' + s2.name);
		}
	},
	
	/**
	 * Add a record to battle log
	 *
	 * @param msg Message
	 */
	add2log : function(msg) {
		$('<div>').html(msg).appendTo(this._log);
	},
	
	/**
	 * Select random enemy type in this area and fight again
	 */
	fightAgain : function() {
		this._log.empty();
		this.start(this._lastAreaEnemies);
	},
	
	/**
	 * Fight the same enemy type in this area
	 */
	fightSameMonsterAgain : function() {
		this._log.empty();
		this.start(this._lastEnemyType);
	}
};